package com.northpool.operator.common.datasource;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.AlgorithmBase;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.common.database.PasswordCrypo;
import com.northpool.operator.common.database.bean.NacosProperties;
import com.northpool.operator.common.test.ParamTestOperator;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@OperatorInfo(
        ref = "DataSourceEncryptInfoOperator",
        version = "1.0.0-1",
        name = "数据源信息获取",
        description = "数据中心加密数据源信息获取（单个数据源）"
)
@OutputParams({
        @OutputParam(ref = "dataSourceInfo", name = "加密数据源信息")
})
public class DataSourceEncryptInfoOperator1 extends AlgorithmBase {

    @InputParams({
            @InputParam(ref = "serverAddr", name = "Nacos服务地址", desc = "Nacos服务地址, 示例: 192.168.164.45:8848", required = true),
            @InputParam(ref = "namespace", name = "命名空间", desc = "Nacos命名空间，示例: dev", required = true),
            @InputParam(ref = "username", name = "用户名", desc = "Nacos用户名", required = true),
            @InputParam(ref = "password", name = "密码", desc = "Nacos密码", required = true),
            @InputParam(ref = "group", name = "分组", desc = "Nacos配置分组", required = true),
            @InputParam(ref = "dataCenterId", name = "数据中心数据源ID", desc = "数据中心数据源ID", required = true),
    })
    public DataSourceEncryptInfoOperator1() {
    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(ParamTestOperator.class, TestParamBuilder.aBuilder()
                .param("serverAddr", "192.168.164.45:8848")
                .param("namespace", "dev")
                .param("username", "nacos")
                .param("password", "admin")
                .param("group", "dev")
                .param("dataCenterId", "402881ac93c476360193cb4d554a02fd")
                .build());
    }

    @Override
    public void execute(Map<String, String> params) {
        String dataSourceId = params.get("dataCenterId");
        if (StringUtils.isBlank(dataSourceId)) {
            throw new IllegalArgumentException("dataSourceId 不能为空");
        }
        NacosProperties properties = new NacosProperties();
        properties.setServerAddr(params.get("serverAddr"));
        properties.setNamespace(params.get("namespace"));
        properties.setUsername(params.get("username"));
        properties.setPassword(params.get("password"));
        properties.setGroup(params.get("group"));

        DatabaseUtils.init(properties);

        String dataSourceJson = DatabaseUtils.getDataSourceJson(dataSourceId);
        String encode = PasswordCrypo.encode(dataSourceJson);
        logResult("dataSourceInfo", encode);
    }
}
